package com.singleton.demo06;

public class KeyInfo {
    private int poolSize;
    private int keyMax;
    private int keyMin;
    private int nextKey;

    public KeyInfo(int poolSize) {
        this.poolSize = poolSize;
    }

    public synchronized int getNextKey() {
        if (nextKey > keyMax) {
            retrieveFromDB();
        }
        return nextKey++;
    }

    // 从数据库中查询
    private void retrieveFromDB() {
        // 更新数据库中缓存的下一次取值key的键值
        String updateSQL = "UPDATE KeyTable SET keyValue = keyValue + " + poolSize + " WHERE keyName = 'key1'";
        String querySQL = "SELECT keyValue FROM KeyTable WHERE KeyName = 'key1'";
        // 执行 updateSQL
        // 执行 querySQL
        // 这里示意性的返回一个数字 100;
        int keyFromDB = 20;
        keyMax = keyFromDB;
        keyMin = keyMax - poolSize + 1;
        nextKey = keyMin ;
    }
}
